package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TCRoleAcctInfoMapper;
import cc.rengu.igas.momp.common.dao.impl.TCRoleAcctInfoMapperImpl;
import cc.rengu.igas.momp.common.entity.TCRoleAcctInfo;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.McBankAccountInfoBean;
import cc.rengu.igas.momp.facade.request.QueryInstitutionMcAccountInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryInstitutionMcAccountInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSONArray;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 法人机构营销活动账号查询
 *
 * @author Jinan Liu
 * @date 2020/4/2 17:49
 * @description
 */
public class QueryInstitutionMcAccountInfoService extends RadpService {
    private String globalInstId;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryInstitutionMcAccountInfoRequest queryInstitutionMcAccountInfoRequest = new QueryInstitutionMcAccountInfoRequest();
            ConvertUtil.convertOutput(queryInstitutionMcAccountInfoRequest);

            QueryInstitutionMcAccountInfoResponse queryInstitutionMcAccountInfoResponse = new QueryInstitutionMcAccountInfoResponse();
            queryInstitutionMcAccountInfoResponse.setHeader(queryInstitutionMcAccountInfoRequest.getHeader());
            BizResponse<QueryInstitutionMcAccountInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryInstitutionMcAccountInfoResponse);
            //校验报文
            checkStatus(queryInstitutionMcAccountInfoRequest);
            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalInstId = queryInstitutionMcAccountInfoRequest.getHeader().getInstId();

            /* 检查操作员机构标示 */
            if (StringUtil.isNullorEmpty(globalInstId)) {
                rglog.error("法人机构号<{}>为空!", globalInstId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }


            // 服务调用
            BizResponse<QueryInstitutionMcAccountInfoResponse> bizResponseNew = queryInstitutionMcAccountInfo(xmlTreeUtil, queryInstitutionMcAccountInfoRequest, queryInstitutionMcAccountInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 法人机构营销活动账号查询
     *
     * @param xmlTreeUtil                           内部XML树
     * @param queryInstitutionMcAccountInfoRequest  法人机构营销活动账号查询接口请求报文
     * @param queryInstitutionMcAccountInfoResponse 法人机构营销活动账号查询接口应答报文
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryInstitutionMcAccountInfoResponse>法人机构营销活动账号查询接口应答报文
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 14:50
     */

    private BizResponse<QueryInstitutionMcAccountInfoResponse> queryInstitutionMcAccountInfo(XmlTreeUtil xmlTreeUtil, QueryInstitutionMcAccountInfoRequest queryInstitutionMcAccountInfoRequest, QueryInstitutionMcAccountInfoResponse queryInstitutionMcAccountInfoResponse) throws Exception {

        BizResponse<QueryInstitutionMcAccountInfoResponse> bizResponse = new BizResponse<>();
        List<McBankAccountInfoBean> mcBankAccountInfoBeanList = new ArrayList<>();
        //查询运营库营销账号
        TCRoleAcctInfoMapper tcRoleAcctInfoMapper = new TCRoleAcctInfoMapperImpl();
        List<TCRoleAcctInfo> tcRoleAcctInfoList = tcRoleAcctInfoMapper.selectInstitutionMcAccountByOrgIdAndAccountCategoryList(globalInstId, CommonConstant.MOMP_DEBIT_SUBJECT, queryInstitutionMcAccountInfoRequest.getOrgId());
        if (null != tcRoleAcctInfoList && tcRoleAcctInfoList.size() > 0) {
            mcBankAccountInfoBeanList = tcRoleAcctInfoList.stream().map(this::convertAccCountInfo).collect(Collectors.toList());
        } else {//如果根据上送的机构号查出空的list则返回总行的账户
            List<TCRoleAcctInfo> tcRoleAcctInfoLists = tcRoleAcctInfoMapper.selectInstitutionMcAccountByOrgIdAndAccountCategoryList(globalInstId, CommonConstant.MOMP_DEBIT_SUBJECT, globalInstId.concat(CommonConstant.HEAD_OFFICE_SIX));
            if (null != tcRoleAcctInfoLists && tcRoleAcctInfoLists.size() > 0) {
                mcBankAccountInfoBeanList = tcRoleAcctInfoLists.stream().map(this::convertAccCountInfo).collect(Collectors.toList());
            } else {
                rglog.error("查询总行清算中心机构号<{}>账号失败", globalInstId.concat(CommonConstant.HEAD_OFFICE_SIX));
                throw new BizException(RespCodeEnum.NO_MC_ACCOUNT_INFO_ERROR.getRespCode(), RespCodeEnum.NO_MC_ACCOUNT_INFO_ERROR.getRespDesc());
            }
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryInstitutionMcAccountInfoResponse.setAcctInfoObk(mcBankAccountInfoBeanList);
        queryInstitutionMcAccountInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryInstitutionMcAccountInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryInstitutionMcAccountInfoResponse);

        return bizResponse;
    }

    /**
     * 请求报文校验
     *
     * @param queryInstitutionMcAccountInfoRequest 请求报文
     * @throws Exception
     */
    private void checkStatus(QueryInstitutionMcAccountInfoRequest queryInstitutionMcAccountInfoRequest) throws Exception {
        if (StringUtil.isNullorEmpty(queryInstitutionMcAccountInfoRequest.getOrgId())) {
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

    }

    /**
     * 账户数据库数据与数据类转化
     *
     * @param tcRoleAcctInfo 运营库角色账户信息
     * @return cc.rengu.igas.momp.common.entity.TCRoleAcctInfo 角色账户信息表
     * @author JL PANG
     * @since 2020/7/20
     */
    private McBankAccountInfoBean convertAccCountInfo(TCRoleAcctInfo tcRoleAcctInfo) {
        McBankAccountInfoBean mcBankAccountInfoBean = new McBankAccountInfoBean();
        try {
            /* 实际账号 */
            mcBankAccountInfoBean.setPayAcctNo(tcRoleAcctInfo.getActualAcct());

        } catch (Exception e) {
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcBankAccountInfoBean;
    }
}
